package com.gismart.installrefferer;

import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
import com.unity3d.player.UnityPlayer;
import org.json.JSONException;
import org.json.JSONObject;

public final class Bridge {
    private static final String JSON_PARAM_APP_INSTALL_TIME = "app_install_time";
    private static final String JSON_PARAM_INSTANT_EXPERIENCE_LAUNCHED = "instant_experience_launched";
    private static final String JSON_PARAM_REFERRER_CLICK_TIME = "referrer_click_time";
    private static final String JSON_PARAM_REFERRER_URL = "referrer_url";
    private static final String STORE_AD_JSON = "store_ad_json";
    private static Context ctx;

    public static void SetContext(Context context) {
        ctx = context;
    }

    public static void GetAttributionDetails() {
        Log.d("Attribution", "GetDetails");
        final InstallReferrerClient build = InstallReferrerClient.newBuilder(ctx).build();
        build.startConnection(new InstallReferrerStateListener() { // from class: com.gismart.installrefferer.Bridge.1
            @Override // com.android.installreferrer.api.InstallReferrerStateListener
            public void onInstallReferrerServiceDisconnected() {
            }

            @Override // com.android.installreferrer.api.InstallReferrerStateListener
            public void onInstallReferrerSetupFinished(int i) {
                if (i == 0) {
                    try {
                        ReferrerDetails installReferrer = build.getInstallReferrer();
                        JSONObject jSONObject = new JSONObject();
                        jSONObject.put(Bridge.JSON_PARAM_REFERRER_URL, installReferrer.getInstallReferrer());
                        jSONObject.put(Bridge.JSON_PARAM_REFERRER_CLICK_TIME, installReferrer.getReferrerClickTimestampSeconds());
                        jSONObject.put(Bridge.JSON_PARAM_APP_INSTALL_TIME, installReferrer.getInstallBeginTimestampSeconds());
                        jSONObject.put(Bridge.JSON_PARAM_INSTANT_EXPERIENCE_LAUNCHED, installReferrer.getGooglePlayInstantParam());
                        JSONObject jSONObject2 = new JSONObject();
                        jSONObject2.put(Bridge.STORE_AD_JSON, jSONObject);
                        UnityPlayer.UnitySendMessage("InstallReffererBridge", "AndroidCallback", jSONObject2.toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (JSONException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });
    }
}